package com.freejava.testmongodb.controller;

import com.freejava.testmongodb.dao.MyLoginLogDao;
import com.freejava.testmongodb.entity.MyLoginLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class LoginController {

    @Autowired
    MyLoginLogDao myLoginLogDao;

    /**
     * 模拟登录的过程，只关注最后日志记录部分。
     */
    @PostMapping("/login")
    public void doLogin(@RequestParam(value = "username", required = false) String userName,
                        @RequestParam(value = "password", required = false) String password,
                        @RequestParam(value = "ip", required = false) String ip) {
        // 记录登录日志，省略一些登录验证的代码
        MyLoginLog myLoginLog = new MyLoginLog();
        myLoginLog.setId(1);
        myLoginLog.setUserId("SD23234");
        myLoginLog.setUsername("freejava");
        myLoginLog.setIp("10.135.172.67");
        myLoginLog.setCreated(System.currentTimeMillis() / 1000);

        myLoginLogDao.insert(myLoginLog);

        System.out.println("insert record is:" + myLoginLog);
    }
}
